//
//  DailyDetailVC.swift
//  MM2
//
//  Created by 李成明 on 2022/1/10.
//

import UIKit

private class DetailReplyTitleCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var replyNum: Int = 0 {
        didSet {
            titleLabel.text = String(format: "评论.共%d条", replyNum)
        }
    }
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.font = .systemFont(ofSize: 13)
        return view
    }()
    
    private lazy var bottomLine: UIView = {
        let view = UIView()
        view.backgroundColor = mainColor
        view.alpha = 0.3
        return view
    }()
    
    private func setupUI() {
        contentView.addsubViews([titleLabel, bottomLine])
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(contentView).offset(16)
            make.top.bottom.right.equalTo(contentView)
            make.height.equalTo(50)
        }
        
        bottomLine.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.height.equalTo(0.3)
        }
    }
}

class DailyDetailVC: MViewController {

    init(model: DailyModel) {
        self.model = model
        super.init(nibName: nil, bundle: nil)
        self.bottomInputView.forumId = model.forumId
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()
    }

    private var model: DailyModel
    
    private lazy var navBar: MNavBar = {
        let view = MNavBar(frame: CGRect(x: 0, y: kStateBarHeight, width: kScreenWidth, height: kNaviBarHeight))
        view.back { [weak self] in
            self?.navigationController?.popViewController(animated: true)
        }.title = "动态详情"
        return view
    }()
    
    private lazy var tableView: UITableView = {
        let view = UITableView()
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.estimatedRowHeight = 160
        view.rowHeight = UITableView.automaticDimension
        view.separatorStyle = .none
        view.delegate = self
        view.dataSource = self
        
        let footer = UIView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: kTabBarAndSafeH + 20))
        view.tableFooterView = footer
        
        view.register(DetailContentCell.self, forCellReuseIdentifier: "DetailContentCell")
        view.register(DetailReplyTitleCell.self, forCellReuseIdentifier: "DetailReplyTitleCell")
        view.register(DetailReplyCell.self, forCellReuseIdentifier: "DetailReplyCell")
        return view
    }()
    
    private lazy var bottomInputView: DetailReplyInputView = {
        let view = DetailReplyInputView(frame: CGRect(x: 0, y: kScreenHeight - kTabBarAndSafeH, width: kScreenWidth, height: kTabBarAndSafeH))
        view.commontClick { [weak self] model in
            self?.model.replyList.append(model)
            self?.tableView.reloadData()
        }
        return view
    }()
}

extension DailyDetailVC {
    private func setupUI() {
        view.addsubViews([navBar, tableView, bottomInputView])
        tableView.frame = CGRect(x: 0, y: kStateAndNavH, width: kScreenWidth, height: kScreenHeight - kStateAndNavH)
    }
}

extension DailyDetailVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1 + 1 + model.replyList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: UITableViewCell?
        if indexPath.row == 0 {
            cell = tableView.dequeueReusableCell(withIdentifier: "DetailContentCell")
            let contentCell = (cell as? DetailContentCell)
            contentCell?.model = model
            contentCell?.avatarClick({ [weak self] model in
                let vc = MineVC(userId: model.authorInfo.userId, hideTabbar: true)
                self?.navigationController?.pushViewController(vc, animated: true)
            })
            
        } else if indexPath.row == 1 {
            cell = tableView.dequeueReusableCell(withIdentifier: "DetailReplyTitleCell")
            (cell as? DetailReplyTitleCell)?.replyNum = model.replyList.count
        } else {
            cell = tableView.dequeueReusableCell(withIdentifier: "DetailReplyCell")
            let replymodel = model.replyList[safe: indexPath.row - 2]
            replymodel?.forumId = model.forumId
            
            let replyCell = cell as? DetailReplyCell
            replyCell?.model = replymodel
            
            replyCell?.nameClick({ [weak self] userId in
                let vc = MineVC(userId: userId, hideTabbar: true)
                self?.navigationController?.pushViewController(vc, animated: true)
            })
        }
        return cell ?? UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row > 1 {
            let replyModel = model.replyList[safe: indexPath.row - 2]
            bottomInputView.replyModel = replyModel
            bottomInputView.showUp()
        }
    }
}
